from column import Column


class TableMetaclss(type):
    def __new__(cls, name, parents, attributes):
        if name == "Table":
            return type.__new__(cls, name, parents, attributes)
        if "display_columns" not in attributes:
            attributes["display_columns"] = []
        for n, v in enumerate(attributes["display_columns"]):
            if isinstance(v, str):
                attributes["display_columns"][n] = Column.get_class(v)
        return type.__new__(cls, name, parents, attributes)
                

class Table(object):
    __metaclass__ = TableMetaclss

    table_actions = []
    display_columns = []
    model = None

    def get_objects(self, page=1, limit=10):
        if not self.model:
            raise NotImplementedError()
        return self.model.query.offset((page-1)*limit).limit(limit).all()
    
    def get_objects_count(self):
        if not self.model:
            raise NotImplementedError()
        return self.model.query.count()